package com.tslfl.springaideepseekr1demo.controller;

import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.Map;

/**
 * @(#) DeepSeekR1Controller.java
 * <p>
 * Copyright HeNan biandan. All rights reserved.
 * @folder deepseek
 * @author: ghm
 * @date: 2025-02-10 17:31
 */
@CrossOrigin
@RestController
@RequestMapping("/deepseek")
public class DeepSeekR1Controller {

    private final OllamaChatModel chatModel;

    @Autowired
    public DeepSeekR1Controller(OllamaChatModel chatModel) {
        this.chatModel = chatModel;
    }

    /**
     * 直接输出
     * @param message
     * @return
     */
    @GetMapping("/ai/generate")
    public Map<String,String> generate(@RequestParam(value = "message", defaultValue = "讲个笑话") String message) {
        return Map.of("generation", this.chatModel.call(message));
    }

    /**
     * 流式响应
     * @param message
     * @return
     */
//    @GetMapping(value = "/ai/generateStream", produces = "application/stream;charset=UTF-8")
    @CrossOrigin
    @GetMapping(value = "/ai/generateStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> generateStream(@RequestParam(value = "message", defaultValue = "讲个笑话") String message) {
        return this.chatModel.stream(message);
    }

    @CrossOrigin
    @GetMapping(value = "/ai/generateStream2", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatResponse> generateStream2(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return this.chatModel.stream(prompt);
    }


}
